package com.visionet.school.app.web.repository.member;

import com.visionet.school.app.web.entity.member.MemberRel;
import com.visionet.school.app.web.entity.vo.MemberVo;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.PagingAndSortingRepository;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * Created by dzd on 2017/3/3.
 */
@Repository
public interface MemberRelRepository extends PagingAndSortingRepository<MemberRel,String>,JpaSpecificationExecutor<MemberRel> {


    /**
     * 根据父母成员查询子成员
     * @param relMemberId
     * @return
     */
    @Query("from MemberRel as mr where mr.relMemberId = ?1 and mr.isDeleted = '0'")
    List<MemberRel> findAllStudentByParent(String relMemberId);


    /**
     * 根据父母成员查询子成员
     * @param relMemberId
     * @param relType
     * @return
     */
    @Query("from MemberRel as mr where mr.relMemberId = ?1 and mr.relType = ?2 and mr.isDeleted = '0'")
    MemberRel findOneByParent(String relMemberId, String relType);

    /**
     * 查询
     * @param relType
     * @param studentId
     * @return
     */
    @Query("from MemberRel as mr where mr.isDeleted = '0' and mr.relType = ?1 and mr.memberId = ?2 ")
    List<MemberRel> findListByRelTypeAndStudentId(String relType, String studentId);

    /**
     * 根绝子成员ID查询父母成员
     * @param studentId
     * @return
     */
    @Query("from MemberRel as mr where mr.memberId = ?1 and mr.isDeleted = '0'")
    List<MemberRel> findParentList(String studentId);


    /**
     * 判断宝宝是否有家属绑定了
     * @param memberId
     * @return
     */
    @Query("select count(*) from MemberRel as mr where mr.isDeleted = '0' and mr.memberId = ?1 ")
    int studentHadBind(String memberId);

    /**
     * 根据宝宝ID和家长ID查询关系
     * @param memberId
     * @param studentId
     * @return
     */
    @Query("from MemberRel as mr where mr.isDeleted = '0' and mr.relMemberId = ?1 and mr.memberId = ?2 ")
    MemberRel findOneBymidAndsid(String memberId,String studentId);

    /**
     * 根据家庭成员Id查询孩子Id
     * @param relMemberId
     * @return
     */
    @Query("from MemberRel as mr where mr.relMemberId = ?1 and mr.isDeleted = '0'")
    MemberRel findStudentIdByParent(String relMemberId);

    /**
     * 家庭成员更正亲属信息
     * @param relType
     * @param relMemberId
     */
    @Modifying
    @Query("update MemberRel as mr set mr.relType=?1 where mr.relMemberId = ?2 and mr.isDeleted = '0'")
    public void updateReltype(String relType,String relMemberId);

    List<MemberVo> AllfamilyInfo(String memberId);
}
